import Mock from 'mockjs'
import { param2Obj } from '../utils/myUtils'
var Arr = []
for (var i = 1; i < 100; i++) {

    Arr.push({
        id: Mock.mock('@id'),
        username: Mock.mock('@cname'),
        jiuzhen: Mock.mock('@increment(1000000000)'),
        shenfenzheng: Mock.mock('@id'),
        iphone: Mock.mock('@increment(100000000000)'),
        guahao: "内科-肾内科",
        type: "专家号",
        money: Mock.mock('@integer(60, 100)'),
        status: "已就诊",
        date: Mock.mock('@date("yyyy-MM-dd")'),
        time: Mock.mock('@time'),
        yuyuedate: Mock.mock('@datetime'),
        ids: i

    })
}

export default {

    homeData: config => {
        const { search, page, pageSize } = param2Obj(config.url)
        var res = []
        if (search) {
            res = Arr.filter(item => {
                return item.username.indexOf(search) != -1
            })

        } else {
            res = Arr
        }

        let newRes = res.filter((item, index) => {
            return (page - 1) * pageSize <= index && index < page * pageSize

        })
        return {
            code: 200,
            msg: '请求成功123',
            data: newRes,
            total: Arr.length
        }
    },
    deleteData: config => {
        const { username } = JSON.parse(config.body)

        Arr.forEach((item, index) => {
            if (item.username == username) {
                Arr.splice(index, 1)
            }
        })
        return {
            code: 200,
            msg: "删除成功",
            data: null
        }
    },
    addHomeData: config => {
        // console.log(JSON.parse(config.body ))  
        const {
            username,
            jiuzhen,
            shenfenzheng,
            iphone,
            guahao,
            type,
            money,
            status,
            date,
            time,
            yuyuedate
        } = JSON.parse(config.body)
        Arr.unshift({
            id: Mock.mock('@increment'),
            username: username,
            jiuzhen: jiuzhen,
            shenfenzheng: shenfenzheng,
            iphone: iphone,
            guahao: guahao,
            type: type,
            money: money,
            status: status,
            date: date,
            time: time,
            yuyuedate: yuyuedate
        })
        return {
            code: 200,
            msg: '添加成功',
            data: null
        }
    },
    updateHomeData: config => {
        const {
            id,
            username,
            jiuzhen,
            shenfenzheng,
            iphone,
            guahao,
            type,
            money,
            status,
            date,
            time,
            yuyuedate
        } = JSON.parse(config.body)
        Arr.forEach(item => {
            if (item.id == id) {
                item.id = id,
                    item.username = username,
                    item.jiuzhen = jiuzhen,
                    item.shenfenzheng = shenfenzheng,
                    item.iphone = iphone,
                    item.guahao = guahao,
                    item.type = type,
                    item.money = money,
                    item.status = status,
                    item.date = date,
                    item.time = time,
                    item.yuyuedate = yuyuedate
            }
        })
        return {
            code: 200,
            msg: '修改成功',
            data: null
        }
    }
}